package collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

class  Rect implements Comparable<Rect>{
    private int width;
    private int height;
    public Rect(){

    }


    public Rect(int width, int height){
        this.width=width;
        this.height=height;
        }

   public int compareTo(Rect o) {
        //这里按照矩形的宽度进行比较
       return this.width-o.width;
       }

    @Override
    public String toString() {
        return "Rect{" +
                "width=" + width +
                ", height=" + height +
                '}';
    }
}
/**
 * @Author Justin Parker
 * @Date 2024/7/15 0015 9:44
 * @Version 1.0
 */
public class CollectionSortDemo02 {
    public static void main(String[] args) {
            Rect p1 = new Rect(10,20);
            Rect p2 = new Rect(20,30);
            Rect p3 = new Rect(30,40);
            List<Rect> list = new ArrayList<>();
            list.add(p1);
            list.add(p2);
            list.add(p3);
            Collections.sort(list); //List集合中的元素需要实现Comparable接口
            System.out.println(list);
    }
}
